<?php
namespace Api\Controller;

use Common\Controller\HomebaseController;

class MapController extends HomebaseController
{


    function _initialize()
    {
    }

    function index()
    {
        $lng = I("get.lng", "121.481798");
        $lat = I("get.lat", "31.238845");
        $this->assign("lng", $lng);
        $this->assign("lat", $lat);
        $this->display();
    }
    
    /**
     * 获取跑客实时位置
     */
    function getpnposition2(){
        $dispatch = M("dispatch");
        $dispatch_id = I("dispatch_id");
        $dispatch_obj = $dispatch->where(array('dispatch_id'=>$dispatch_id))->field("longitude,latitude,place_up_time,dispatch_name,mobile")->find();

        if($dispatch_obj){
            echo json_encode($dispatch_obj);
        }
    }                
    /**
     * 异步请求接单区域5公里内跑客信息
     *  order_id 订单id
     */
    function pnRobRouteCounts(){
        $ret = M('order')->where(array('order_id'=>I('order_id')))->field('order_type,dispatch_id,send_latit,send_longit,reci_latit,reci_longit')->find();
        if($ret){
            $latit=$ret['send_latit'];
            $longit=$ret['send_longit'];
            if($ret['order_type']==2 || $ret['order_type']==3){//代买、排队的经纬度附近的跑客
                $latit=$ret['reci_latit'];
                $longit=$ret['reci_longit'];
            }
            $lon=doubleval($longit);
            $lat=doubleval($latit);
            $squares = returnSquarePoint($lon,$lat,5);
            $where = " ";

            $Model = M();
            $sql = "select dispatch_id,mobile,longitude,latitude,dispatch_name,place_up_time".
                //",ROUND(6378.138*2*ASIN(SQRT(POW(SIN((22.299439*PI()/180-".$lat."*PI()/180)/2),2)+COS(22.299439*PI()/180)*COS(".$lat."*PI()/180)*POW(SIN((114.173881*PI()/180-".$lon."*PI()/180)/2),2)))*1000) AS juli".
                " from db_dispatch d where  latitude>{$squares['right-bottom']['lat']} and latitude < {$squares['left-top']['lat']} and longitude > {$squares['left-top']['lng']} and longitude < {$squares['right-bottom']['lng']} ".
                " and work_status = 1 and is_sign=2 ";
            //     $where.
            //  " and  (SELECT count(dispatch_id) as count FROM db_order o WHERE o.dispatch_id =d.dispatch_id   and o.status >1 and o.status < 7 and is_reservation=0) = 0".
            //  " ORDER BY ROUND(6378.138*2*ASIN(SQRT(POW(SIN((22.299439*PI()/180-".$lat."*PI()/180)/2),2)+COS(22.299439*PI()/180)*COS(".$lat."*PI()/180)*POW(SIN((114.173881*PI()/180-".$lon."*PI()/180)/2),2)))*1000),praise desc,todaytime desc".
            ;
//    dump($sql);
            $voList = $Model->query($sql);
//    dump($voList[0]['dispatch_id']);
            // dump($voList);
            $pnret['otherpn']=$voList;
            $pnret['pnlongitude']=$longit;
            $pnret['pnlatitude'] =$latit;
            if($pnret){
                echo json_encode($pnret);
            }
        }
    }

    /**
     * 郑州市内跑客信息页面
     *  order_id 订单id
     */
    function allpnroutecounts(){
        $this->display();
    }
    /**
     * 异步请求郑州市内跑客信息
     *  order_id 订单id
     */
    function allpnroute(){

        $dispatch_db = M('dispatch');
        $dispatch_obj = $dispatch_db->field('dispatch_id,mobile,longitude,latitude,dispatch_name,place_up_time')->where(array('work_status'=>1,'is_sign'=>2)) ->select();

        if($dispatch_obj){
            echo json_encode($dispatch_obj);
        }

    }
    /**
     * @api {get} user_order_map/order_id/order_id  用户查询自己订单的状态,距离,时间
     * @order_id 订单ID
     * @apiGroup pt
     * @start 起点经纬度
     * @end   终点经纬度  
     * @paoke_now_location 跑客当前的经纬度
     * @data["status"]    订单状态(数字)
     * @data["distance"]   订单距离
     * @data["duration"]   订单所需的时间
     * @data["distance_type"] 订单类型(汉字)
     * @order_id 订单ID
     *
     * 用户端的地图（取送订单）
     * 2.30秒刷新一下跑客位置
     * 12.12之后用
     **/
    function  user_order_map()
   { 
        $order_id = I("order_id");
        $order = M("order");
        $dispatch = M("dispatch");
        $data = $order->where(array('order_id'=>$order_id))->field('send_latit,send_longit,reci_latit,reci_longit,order_type,dispatch_id,status,pay_status')->find();
//        dump($data);
//        die;
        $data['dispatch'] = $dispatch->where(array('dispatch_id'=>$data['dispatch_id']))->field("longitude,latitude,place_up_time")->find();
        //dump($data);
        //获取订单的起点
        $start_latit = $data['send_latit'];
        $start_longit = $data['send_longit'];
        $start = "$start_latit,$start_longit";
        //终点
        $end_latit = $data['reci_latit'];
        $end_longit = $data['reci_longit'];
        $end = "$end_latit,$end_longit";
        
        //获取跑客的位置
        $paoke_longitude = $data['dispatch']['longitude'];
        $paoke_latitude = $data['dispatch']['latitude'];
        $paoke_now_location = "$paoke_latitude,$paoke_longitude";
        //判断订单支付状态
       if($data["pay_status"] == 1)
     {
        //判断订单类型
        if($data["order_type"] == 1 || $data["order_type"] == 2)
        {
            
                    //根据根据订单支付状态,判断$data["status"]订单状态,并获取订单的距离,时间
                        if($data["status"] == 0)
                        {
            //                 $ret = line($start, $end);
            //                 $ret2 = $ret['result'];
            //                 $data["distance"] = $ret2[0]["distance"]["text"];
            //                 $data["duration"] = $ret2[0]["duration"]["text"];
               //              $data["distance"] = substr($data["distance"],0,-6);
               //              $data["duration"] = substr($data["duration"],0,-6);
                             $data["distance_type"] = "未处理";
                        }elseif ($data["status"] == 1)
                        {
                            $data["distance_type"] = "待分配";
                        }elseif(2 <= $data["status"] && $data["status"] <= 4)
                        {
                              //百度路线距离
                              $ret = line($paoke_now_location,$end);
                              //把获取到的值转换为需要的格式
                               $ret2 = $ret['result'];
                               //跑客距离目的地的距离
                               $data["distance"] = $ret2[0]["distance"]["text"];
                               //跑客到达地点的时间
                               $data["duration2"] = $ret2[0]["duration"]["text"];
                               //把获取到的值转换为需要的格式
                               $data["distance"] = substr($data["distance"],0,-6);
                               $data["duration"] = substr($data["duration2"],0,-6);
                               $data["time"] = substr($data["duration2"],-6);
                               $data["distance_type"] = "取件";

                         }elseif(5 == $data["status"])
                         {
                             if( $paoke_now_location != $start)
                             {   
                               $ret = line($start, $paoke_now_location);
                               //把获取到的值转换为需要的格式
                               $ret2 = $ret['result'];
                               $data["distance"] = $ret2[0]["distance"]["text"];
                               $data["duration2"] = $ret2[0]["duration"]["text"];

                               $data["distance"] = substr($data["distance"],0,-6);
                               $data["duration"] = substr($data["duration2"],0,-6);
                               $data["time"] = substr($data["duration2"],-6);
                               $data["distance_type"] = "送件";
                               //$data2 = "您好,您的物品已取到正在返回的路上,距离目的地还有".$ret4["distance"]."还需要".$ret4["duration"];
                             }
                         }elseif($data["status"] == 6)
                         {
                           $data["distance_type"] = "已送达";
                         }elseif($data["status"] == 7)
                         {
                             $data["distance_type"] = "已完成";
                         }elseif($data["status"] == 9)
                         {
                             $data["distance_type"] = "已取消";
                         }
        }else
        {
                    if($data["status"] == 0)
                    {
                         $data["distance_type"] = "未处理";
                    }elseif($data["status"] == 1)
                    {
                       $data["distance_type"] = "待分配";
                    }elseif(2 == $data["status"])
                    {

                           $ret = line($paoke_now_location,$end);

                           //把获取到的值转换为需要的格式
                           $ret2 = $ret['result'];
                           $data["distance"] = $ret2[0]["distance"]["text"];
                           $data["duration2"] = $ret2[0]["duration"]["text"];
                           $data["distance"] = substr($data["distance"],0,-6);
                           $data["duration"] = substr($data["duration2"],0,-6);
                           $data["time"] = substr($data["duration2"],-6);
                           $data["distance_type"] = "去排队地点";
                    }elseif($data["status"] == 3)
                    {
                            $data["distance_type"] = "到达排队地点";
                    }
                    elseif($data["status"] == 7)
                    {
                         $data["distance"] = 0;
                         $data["duration"] = 0;
                         $data["distance_type"] = "已完成";
                    }else
                    {
                       $data["distance_type"] = "已取消";
                    }

        }
     } else {
          $data["distance_type"] = "未分配";
     }
  
     //订单取消或者完成
        if($data['status'] == "7" || $data['status'] == "9"){
            $data['dispatch']['longitude'] ="0";
            $data['dispatch']['latitude'] ="0";
        }else{
            if(empty($data['dispatch']['longitude'])){
                $data['dispatch']['longitude'] ="0";
                $data['dispatch']['latitude'] ="0";
            }
        }
        if($data['order_type']==3){
            $data['send_latit']=0;
            $data['send_longit']=0;
        }

        $this->assign('order_map',$data);
        //dump($data);
        $this->display();
   }   
   /**
     * 获取跑客实时位置
     */
    function getpnposition(){
        $dispatch = M("dispatch");
        $dispatch_id = I("dispatch_id");
        if($dispatch_id)
        {   
            $dispatch_obj = $dispatch->where(array('dispatch_id'=>$dispatch_id))->field("longitude,latitude,place_up_time,dispatch_name,mobile")->find();

            if($dispatch_obj){
                echo json_encode($dispatch_obj);
            }
        }else
        {
            $false = "false";
            echo json_encode($false);
        }
    }  
    
      
}